home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / cvs-1_3.lha / cvs-1.3 / lib / rename.c < prev    next >
C/C++ Source or Header  |  1991-09-30  |  2KB  |  69 lines

  1. /* rename.c -- BSD compatible directory function for System V
  2.    Copyright (C) 1988, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include <sys/types.h>
  19. #include <sys/stat.h>
  20. #include <errno.h>
  21. #ifndef STDC_HEADERS
  22. extern int errno;
  23. #endif
  24.  
  25. /* Rename file FROM to file TO.
  26.    Return 0 if successful, -1 if not. */
  27.  
  28. int
  29. rename (from, to)
  30.      char *from;
  31.      char *to;
  32. {
  33.   struct stat from_stats;
  34.   int pid, status;
  35.  
  36.   if (stat (from, &from_stats) == 0)
  37.     {
  38.       if (unlink (to) && errno != ENOENT)
  39.     return -1;
  40.       if ((from_stats.st_mode & S_IFMT) == S_IFDIR)
  41.     {
  42.       /* Need a setuid root process to link and unlink directories. */
  43.       pid = fork ();
  44.       switch (pid)
  45.         {
  46.         case -1:        /* Error. */
  47.           error (1, errno, "cannot fork");
  48.  
  49.         case 0:        /* Child. */
  50.           execl (MVDIR, "mvdir", from, to, (char *) 0);
  51.           error (255, errno, "cannot run `%s'", MVDIR);
  52.  
  53.         default:        /* Parent. */
  54.           while (wait (&status) != pid)
  55.         /* Do nothing. */ ;
  56.  
  57.           errno = 0;    /* mvdir printed the system error message. */
  58.           return status != 0 ? -1 : 0;
  59.         }
  60.     }
  61.       else
  62.     {
  63.       if (link (from, to) == 0 && (unlink (from) == 0 || errno == ENOENT))
  64.         return 0;
  65.     }
  66.     }
  67.   return -1;
  68. }
  69.